package ar.com.personalTravellers.rowProcessors;

import ar.com.personalTravellers.beans.Hotel;
import ar.com.personalTravellers.beans.Room;
import ar.com.personalTravellers.daos.factory.PTDAOFactory;
import com.angel.base.helpers.StringHelper;
import com.angel.io.exceptions.InvalidRowDataException;
import com.angel.io.parameters.ParametersService;
import com.angel.io.processors.imports.ImportRowProcessorCommand;
import com.angel.io.type.rows.impl.HeaderRowFile;
import com.angel.io.type.rows.interfaces.ImportRowFile;

/**
 *
 * @author William
 */
public class HotelRoomsRowProcessorCommand implements ImportRowProcessorCommand {

    public static final String HOTEL_NAME_COLUMN = "hotelName";
	public static final String ROOM_NAME_COLUMN = "roomName";

    public static final String DAO_FACTORY_PARAMETER_SERVICE_NAME = "daoFactoryHotelRooms";

    public void checkRowData(ImportRowFile importRowFile, HeaderRowFile headerRowFile) throws InvalidRowDataException {
        String roomName = importRowFile.getStringValue(headerRowFile.getColumnPosition(ROOM_NAME_COLUMN));
		String hotelName = importRowFile.getStringValue(headerRowFile.getColumnPosition(HOTEL_NAME_COLUMN));

        if( StringHelper.isEmpty(roomName)              || StringHelper.isEmpty(hotelName)){
			throw new InvalidRowDataException("Some row data are NULL - " +
					"roomName: [" + roomName + "] - " +
					"hotelName: [" + hotelName  + "] - ");
		}
    }

    public Object processRow(ImportRowFile importRowFile, HeaderRowFile headerRowFile, ParametersService parametersService) {
        String roomName = importRowFile.getStringValue(headerRowFile.getColumnPosition(ROOM_NAME_COLUMN));
		String hotelName = importRowFile.getStringValue(headerRowFile.getColumnPosition(HOTEL_NAME_COLUMN));

        PTDAOFactory daoFactory = (PTDAOFactory) parametersService.findParameter(DAO_FACTORY_PARAMETER_SERVICE_NAME);

        Hotel hotel = daoFactory.getHotelDAO().findUniqueByName(hotelName);
        Room room = daoFactory.getRoomDAO().findUniqueByName(roomName);

        hotel.addRoom(room);
        return hotel;
    }

    public String[] getHeaderColumnsNames() {
        return new String[] {HOTEL_NAME_COLUMN, ROOM_NAME_COLUMN};
    }
}